package com.geek.javase.ch01.test;

/*
 *@author wangpeng
 *2025/10/9 17:28
 *@description TODO
 */

//成绩计算类

public class GradeCalculator {
    //三个float类型的私有成员属性
    private float javaScore;     // Java成绩
    private float csharpScore;   // C#成绩
    private float databaseScore; // 数据库成绩

    //无参构造方法
    public GradeCalculator() {
        // 初始化默认成绩为0
        /*this.javaScore = 0.0f;
        this.csharpScore = 0.0f;
        this.databaseScore = 0.0f;*/
    }

    // 有参数的构造方法
    public GradeCalculator(float javaScore, float csharpScore, float databaseScore) {
        this.javaScore = javaScore;
        this.csharpScore = csharpScore;
        this.databaseScore = databaseScore;
    }

    //公开的getter和setter方法
    //获取Java成绩
    public float getJavaScore() {
        return javaScore;
    }

    //设置Java成绩
    public void setJavaScore(float javaScore) {
        this.javaScore = javaScore;
    }

    //获取C#成绩
    public float getCsharpScore() {
        return csharpScore;
    }

    //设置C#成绩
    public void setCsharpScore(float csharpScore) {
        this.csharpScore = csharpScore;
    }

    //获取数据库成绩
    public float getDatabaseScore() {
        return databaseScore;
    }

    //设置数据库成绩
    public void setDatabaseScore(float databaseScore) {
        this.databaseScore = databaseScore;
    }

    //计算三个科目的平均值的方法计算三门课程的平均成绩
    public float calculateAverage() {
        // 通过getter方法获取属性值进行计算
        float total = getJavaScore() + getCsharpScore() + getDatabaseScore();
        return total / 3.0f;
    }

    //计算三个科目的总成绩的方法
    public float calculateTotal() {
        // 通过getter方法获取属性值进行计算
        return getJavaScore() + getCsharpScore() + getDatabaseScore();
    }

    //显示所有成绩信息
    public void displayScores() {
        System.out.println("=== 成绩信息 ===");
        System.out.println("Java成绩: " + getJavaScore());
        System.out.println("C#成绩: " + getCsharpScore());
        System.out.println("数据库成绩: " + getDatabaseScore());
        System.out.println("总成绩: " + calculateTotal());
        System.out.println("平均成绩: " + String.format("%.2f", calculateAverage()));
        System.out.println("==============");
    }

    //程序入口main方法
    public static void main(String[] args) {
        System.out.println("=== 成绩计算类测试 ===\n");

        // 通过无参构造方法实例化对象
        System.out.println("--- 测试: 使用无参构造方法 ---");
        GradeCalculator student1 = new GradeCalculator();

        // 使用setter方法设置成绩
        student1.setJavaScore(85.5f);
        student1.setCsharpScore(78.0f);
        student1.setDatabaseScore(92.5f);

        // 通过对象调用计算方法并打印输出
        System.out.println("单独调用计算方法:");
        System.out.println("总成绩: " + student1.calculateTotal());
        System.out.println("平均成绩: " + String.format("%.2f", student1.calculateAverage()));
        // 显示成绩信息
        student1.displayScores();

        // 通过带参构造方法实例化对象
        System.out.println("\n--- 测试: 使用带参构造方法 ---");
        GradeCalculator student2 = new GradeCalculator(95.0f, 88.5f, 91.0f);
        // 通过对象调用计算方法并打印输出
        System.out.println("单独调用计算方法:");
        System.out.println("总成绩: " + student2.calculateTotal());
        System.out.println("平均成绩: " + String.format("%.2f", student2.calculateAverage()));

        // 显示成绩信息
        student2.displayScores();
    }
}